﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace SuperKit
{
    class RenameParentDateBrand : CommandExecutor {
        public int TYPE_COPY_NAME = 0;
        public int TYPE_PICK_NAME = 1;

        public RenameParentDateBrand(ArgReader arg, IExecutorOwner form, string fpath) : base(arg, form, fpath) {
        }

        public override void Execute() {
            base.Execute();
            int type = Arg.TypeId;
            if (type == TYPE_COPY_NAME) {
                OnCopyName();
            }else if (type == TYPE_PICK_NAME) {
                OnPickName();
            }

            /*var dateIdx = newName.IndexOf("]");
            if (dateIdx == -1) {
                Error("此项没有日期：{0}", newName);
                return;
            }
            string dateStr = newName.Substring(0, dateIdx + 1);
            string leftName = newName.Substring(dateIdx + 1);
            var brandIdx = leftName.IndexOf("]");
            if (brandIdx == -1) {
                Error("此项没有BRAND：{0}", leftName);
                return;
            }
            string brandStr = leftName.Substring(0, brandIdx + 1);

            string dirName = Path.GetFileName(Dir);
            string finalDir = string.Format("{0}{1}{2}", dateStr, brandStr, dirName);
            string finalPath = Path.Combine(Path.GetDirectoryName(Dir), finalDir);

            //Notice("最终目录名：{0}", finalDir);
            Notice("最终改名（从）：{0}", Dir);
            Notice("最终改名（到）：{0}", finalPath);

            if (Directory.Exists(finalPath)) {
                Error("目标目录名已经存在：{0}", finalPath);
                return;
            }*/

            //Directory.Move(Dir, finalPath);


            End();
        }

        private void OnPickName() {
            DirectoryInfo dinfo = new DirectoryInfo(FilePath);
            var max = PickMaxZip(dinfo);
            if (max == null) {
                Error("目录中没有文件！");
                return;
            }
            var newName = AdjustName(Path.GetFileNameWithoutExtension(max.Name), ReplacerDict.GalgameReplacer);
            var finalPath = Path.Combine(Dir, newName);
            if (Directory.Exists(finalPath)) {
                Error("最终目录已经存在：{0}", finalPath);
            } else {
                Directory.Move(FilePath, finalPath);
            }
            Flush("Change to:{0}", finalPath);
        }

        private string PickUpName(FileInfo max) {
            var newName = AdjustName(Path.GetFileNameWithoutExtension(max.Name), ReplacerDict.GalgameReplacer);
            var dateIdx = newName.IndexOf("]");
            if (dateIdx == -1) {
                Error("此项没有日期：{0}", newName);
                return Kit.EMPTY;
            }
            string dateStr = newName.Substring(0, dateIdx + 1);
            string leftName = newName.Substring(dateIdx + 1);
            var brandIdx = leftName.IndexOf("]");
            if (brandIdx == -1) {
                Error("此项没有BRAND：{0}", leftName);
                return Kit.EMPTY;
            }
            string brandStr = leftName.Substring(0, brandIdx + 1);

            string dirName = Path.GetFileName(Dir);
            string finalDir = string.Format("{0}{1}{2}", dateStr, brandStr, dirName);
            string finalPath = Path.Combine(Path.GetDirectoryName(Dir), finalDir);

            //Notice("最终目录名：{0}", finalDir);
            Notice("最终改名（从）：{0}", Dir);
            Notice("最终改名（到）：{0}", finalPath);

            if (Directory.Exists(finalPath)) {
                Error("目标目录名已经存在：{0}", finalPath);
                return Kit.EMPTY;
            }

            return finalPath;
        }

        private FileInfo PickMaxZip(DirectoryInfo dinfo) {
            FileInfo max = null;            
            foreach (var i in dinfo.GetFiles()) {
                if (IsZip(i)) {
                    if (max == null) {
                        max = i;
                    } else {
                        if (i.Length > max.Length) {
                            max = i;
                        }
                    }
                }
            }
            return max;
        }

        private void OnCopyName() {
            string newName = AdjustName(PureName, ReplacerDict.GalgameReplacer);
            Clipboard.SetDataObject(newName);
        }
    }
}
